import { webUrl,api } from "./config.js"
import Common from "./common.js"
// 网络监听
const NetWork = {
	// 网络状态
	isConnect:false,
	// 监听网络状态
	On(){
		// 获取当前网络状态
		uni.getNetworkType({
			success: (res) => {
				if(res.networkType!=='none'){ this.isConnect=true; return;}
				uni.showToast({
					icon:"none",
					title: '请先连接网络',
				});
			}
		})
		// 监听网络状态变化
		uni.onNetworkStatusChange((res)=>{
			this.isConnect = res.isConnected;
			if(!res.isConnected){
				uni.showToast({
					icon:"none",
					title: '您目前处于断网状态',
				});
			}
		})
	}
}


 
// app更新
const Update = function(){
	// #ifdef APP-PLUS  
  let progrss = 0;
  let total = 0;
  let timer = null;
  let show = null;
  // 检测版本
	plus.runtime.getProperty(plus.runtime.appid, function(e) {  
    const {version} = e;
    
		uni.request({  
			url: webUrl + api.getSetting,  
			success: (result) => {  
				let {data} = result.data;  
        let new_version = data.version.slice(1)
        
        if(new_version === version) return;  
        // 判断是否更新
        if(data.is_new && data.new_url){
          // 弹出提示框
          plus.nativeUI.confirm("已檢測到新版本，該版本有重大更新，請立即更新", function(e){
            show = plus.nativeUI.showWaiting("安裝更新文件中...");          
            
            // 下载更新包
            let downloadTask = uni.downloadFile({
              url: data.new_url,  
              success: (downloadResult) => {    
                if (downloadResult.statusCode === 200) {  
                  // 下载
                  plus.runtime.install(downloadResult.tempFilePath, {  
                    force: true  
                  }, function() {  
                    console.log('install success...');  
                    plus.nativeUI.closeWaiting();
                    // 弹出重启框
                    plus.nativeUI.confirm("最新版安装完成，請立即重啟",(e)=>{
                      // 重启
                      plus.runtime.restart();  
                    },{
                      "title": "重啟提示",
                      "buttons": ["立即重啟"]
                    })
                  }, function(e) {  
                    console.error('install fail...');  
                  });  
                }  
              },
              fail: ()=>{
                console.log('fail')
              }
            });  
            
            downloadTask.onProgressUpdate((res) => {
              
              // console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);

               if(!timer){
                  timer = setTimeout(()=>{
                    clearTimeout(timer);
                    total = Common.change(res.totalBytesExpectedToWrite); 
                    progrss = Common.change(res.totalBytesWritten);
                    show.setTitle(`更新进度: ${progrss}/${total}`)
                    timer = null; 
                  },500)
                }
             });
             
             
          },{
            "title": '更新提示',
            "buttons": ["立即更新"] 
          });
        }
			}  
		});  
	});  
	// #endif  
}


export default {
	NetWork,
	Update
}